﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using WebSysCore.JSons;
using WebSysCore;
using WebSysCore.Helpers;
using HSchSchedule.Utils;

namespace HSchSchedule.AjaxPages
{
    public partial class AjaxAddSubjectNotAt : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod(true)]
        public static string AddSubjectNotAt(List<JSubjectNotAt> lstJSNA)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    bool ok = true;
                    string err = "";
                    foreach (JSubjectNotAt js in lstJSNA)
                    {
                        if (!ValidateHelp.ValidateSNA(js, schId, out err))
                        {
                            ok = false;
                            break;
                        }

                    }
                    if (ok)
                    {
                        List<SubjectNotAt> lstSNA = new List<SubjectNotAt>();
                        for (int i = 0; i < lstJSNA.Count; i++)
                        {
                            SubjectNotAt d = AssignHelp.JSubNotAtToSubNotAt(lstJSNA[i]);
                            d.ScheduleInListId = schId;
                            lstSNA.Add(d);
                        }
                        DataBase.InsertSubjectNotAt(lstSNA);
                        return GUIConstants.SUCCESS_RESULT;
                    }
                    else
                    {
                        return GUIConstants.FAIL_RESULT;
                    }
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }


        }


        [WebMethod(true)]
        public static string GetLessions()
        {
            //List<Lession> lstLes=Data
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<Lession> lstLes = DataBase.GetAllLession(schId);
                    List<JLession> lstJLes = new List<JLession>();
                    foreach (Lession l in lstLes)
                    {
                        JLession jl = AssignHelp.LessionToJLession(l);
                        lstJLes.Add(jl);
                    }
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    return serializer.Serialize(lstJLes);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string GetSubjects()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<Subject> lstSub = DataBase.GetAllSubject(schId);
                    List<JSubject> lstJSub = new List<JSubject>();
                    foreach (Subject s in lstSub)
                    {
                        JSubject js = AssignHelp.SubToJSub(s);
                        lstJSub.Add(js);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJSub);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string GetSessions()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<Session> lstSes = DataBase.GetAllSession(schId);
                    List<JSession> lstJSes = new List<JSession>();
                    foreach (Session s in lstSes)
                    {
                        JSession js = AssignHelp.SessionToJSession(s);
                        lstJSes.Add(js);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJSes);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string GetDays()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<Day> lstDay = DataBase.GetAllDay(schId);
                    List<JDay> lstJDay = new List<JDay>();
                    foreach (Day d in lstDay)
                    {
                        JDay jd = AssignHelp.DayToJDay(d);
                        lstJDay.Add(jd);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJDay);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string GetSNA(int subjectID)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<SubjectNotAt> lstSNA = DataBase.GetSubjectNotAt(schId, subjectID);
                    List<JSubjectNotAt> lstJSNA = new List<JSubjectNotAt>();
                    foreach (SubjectNotAt sna in lstSNA)
                    {
                        JSubjectNotAt jsna = AssignHelp.SubNotAtToJSubNotAt(sna);
                        lstJSNA.Add(jsna);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJSNA);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }

    }
}
